package com.wind.taobao.api.trade.shipping.convert
{
	import com.wind.taobao.api.trade.shipping.model.LogisticCompany;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * LogisticCompany转换器
	 * @author fxy
	 *
	 */
	public class LogisticCompanyConvert
	{
		/**
		 * 转换LogisticCompany集合
		 */
		public static function convertLogisticCompanys(value:Object, type:String="json"):ArrayCollection
		{
			var logisticCompanys:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							logisticCompanys.addItem(convertLogisticCompany((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return logisticCompanys;
		}

		/**
		 * 转换单个LogisticCompany对象
		 * value:XML 或 JSON
		 */
		public static function convertLogisticCompany(value:Object, type:String="json", logisticCompany:LogisticCompany=null):LogisticCompany
		{
			if (logisticCompany == null)
				logisticCompany=new LogisticCompany();
			if (value == null)
				return logisticCompany;
			if (type == TaobaoConstants.XML)
			{
				logisticCompany=convertXMLToLogisticCompany(XML(value), logisticCompany);
			}
			else
			{
				logisticCompany=convertJSONToLogisticCompany(value, logisticCompany);
			}
			return logisticCompany;
		}

		/**
		 * JSON转换为LogisticCompany
		 */
		private static function convertJSONToLogisticCompany(json:Object, logisticCompany:LogisticCompany):LogisticCompany
		{
			if (json.hasOwnProperty(TaobaoConstants.COMPANY_ID))
			{
				logisticCompany.company_id=json[TaobaoConstants.COMPANY_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.COMPANY_CODE))
			{
				logisticCompany.company_code=json[TaobaoConstants.COMPANY_CODE];
			}
			if (json.hasOwnProperty(TaobaoConstants.COMPANY_NAME))
			{
				logisticCompany.company_name=json[TaobaoConstants.COMPANY_NAME];
			}

			return logisticCompany;
		}



		/**
		 * XML转换为LogisticCompany
		 */
		private static function convertXMLToLogisticCompany(value:XML, logisticCompany:LogisticCompany):LogisticCompany
		{
			return logisticCompany;
		}
	}
}